var _data;

function _defineProperty(t, a, e) {
    return a in t ? Object.defineProperty(t, a, {
        value: e,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : t[a] = e, t;
}

var app = getApp(), util = require("../../../resource/js/util.js"), $ = require("../../../resource/utils/underscore"), WxParse = require("../../../resource/wxParse/wxParse.js"), _function = require("../../../resource/function/function.js"), playKey = "play_info";

Page({
    timeId: 0,
    data: (_data = {
        datum: [],
        userInfo: [],
        comment: [],
        mypay: null,
        member: 0,
        zan: 0,
        pay_num: 0,
        oldzan: 0,
        collect: 0,
        related: [],
        id: 0,
        continues: !1,
        become: !1
    }, _defineProperty(_data, "userInfo", []), _defineProperty(_data, "tk", !1), _defineProperty(_data, "p", 1), 
    _defineProperty(_data, "pnum", 6), _defineProperty(_data, "is_pay", !1), _defineProperty(_data, "shang", !1), 
    _defineProperty(_data, "shang_num", 0), _defineProperty(_data, "currentStop", 0), 
    _defineProperty(_data, "pay_status", 0), _defineProperty(_data, "is_avAutoPlay", 0), 
    _defineProperty(_data, "playStatus", 0), _defineProperty(_data, "stepTime", "00:00"), 
    _defineProperty(_data, "durationTime", "00:00"), _defineProperty(_data, "progress", "0"), 
    _defineProperty(_data, "report_tan", !1), _defineProperty(_data, "radio_value", 0), 
    _data),
    onLoad: function(e) {
        var i = this;
        _function.system(this), e.fxid && e.fxtype && (wx.setStorageSync("fxid", e.fxid), 
        _function.request("entry/wxapp/Judge", {
            uid: this.data.userInfo.id,
            id: e.id
        }, "", function(t) {
            if (0 == t && i.data.userInfo.author != i.data.datum.article.author_id) return _function.hint(3, "抱歉,该音乐仅限指定分组用户聆听^_^!", "温馨提示", function(t) {}), 
            wx.redirectTo({
                url: "dg_costread/pages/newcms/index/index"
            }), !1;
        }, this));
        var t = wx.getStorageSync("is_pay");
        this.setData({
            id: e.id,
            is_pay: 1 == t
        });
        var a = wx.getStorageSync("userInfo");
        a ? (this.setData({
            userInfo: a
        }), this.getDatum(e.id)) : _function.getUserinfo(this), wx.onBackgroundAudioPause(this.onPause), 
        wx.onBackgroundAudioStop(this.onStop), wx.getBackgroundAudioPlayerState({
            success: function(t) {
                var a = wx.getStorageSync(playKey);
                t.dataUrl;
                a && 1 == t.status && e.id == a.id && (i.startProgressListener(), i.onProgressUpdate(t));
            }
        });
    },
    onPlay: function() {
        var a = this;
        if (0 < this.timeId ? wx.setNavigationBarTitle({
            title: "Sazim 音乐"
        }) : wx.setNavigationBarTitle({
            title: "音频加载中..."
        }), this.setData({
            playStatus: 1
        }), wx.setStorageSync(playKey, {
            id: this.data.id,
            path: this.data.datum.article.bg_music
        }), this.data.member) this.startProgressListener(); else if (1 != this.data.pay_status || this.data.mypay) this.startProgressListener(); else if (this.timeId > this.data.currentStop) {
            if (3 == this.data.datum.article.types && this.data.userInfo.author != this.data.datum.article.author_id) return _function.hint(3, "完成支付继续收听哦^_^!", "温馨提示", function(t) {
                wx.pauseBackgroundAudio({
                    success: function(t) {
                        a.setData({
                            continues: !0
                        }), a.setData({
                            playStatus: 0
                        }), a.stopProgressListener();
                    },
                    fail: function(t) {
                        a.setData({
                            continues: !0
                        }), a.setData({
                            playStatus: 0
                        }), a.stopProgressListener();
                    }
                });
            }), !1;
        } else this.startProgressListener();
    },
    onPause: function() {
        this.setData({
            playStatus: 0
        }), this.stopProgressListener();
    },
    onStop: function(t) {
        this.setData({
            playStatus: 0
        }), this.stopProgressListener(), wx.removeStorageSync(playKey), wx.getBackgroundAudioPlayerState({
            success: this.onProgressUpdate
        });
    },
    onUnload: function() {
        3 == this.data.datum.article.types && wx.setStorageSync("music", this.data.datum.article.id), 
        this.data.member || 1 != this.data.pay_status || this.data.mypay || 3 == this.data.datum.article.types && this.data.userInfo.author != this.data.datum.article.author_id && wx.stopBackgroundAudio(), 
        this.stopProgressListener();
    },
    startProgressListener: function() {
        var t = this, a = function() {
            wx.getBackgroundAudioPlayerState({
                success: t.onProgressUpdate
            });
        };
        this.timeId = setInterval(a, 250), a();
    },
    stopProgressListener: function() {
        clearInterval(this.timeId);
    },
    onProgressUpdate: function(t) {
        var a = this;
        this.playUrl = t.dataUrl;
        var e = t.currentPosition, i = t.duration, n = Math.floor(e / 60), s = Math.floor(e % 60), r = Math.floor(i / 60), o = Math.floor(i % 60), d = {
            stepTime: (n < 10 ? "0" + n : n) + ":" + (s < 10 ? "0" + s : s),
            durationTime: (r < 10 ? "0" + r : r) + ":" + (o < 10 ? "0" + o : o),
            progress: e / i * 100,
            downloadPercent: t.downloadPercent
        };
        if (0 != t.status && 1 != t.status || (d.playStatus = t.status), this.setData(d), 
        0 < e && wx.setNavigationBarTitle({
            title: "Sazim 音乐"
        }), !this.data.member && 1 == this.data.pay_status && !this.data.mypay && e > this.data.currentStop && 3 == this.data.datum.article.types && this.data.userInfo.author != this.data.datum.article.author_id) return _function.hint(3, "完成支付继续收听哦^_^!", "温馨提示", function(t) {
            wx.pauseBackgroundAudio({
                success: function(t) {
                    a.setData({
                        continues: !0
                    }), a.setData({
                        playStatus: 0
                    }), a.stopProgressListener();
                },
                fail: function(t) {
                    a.setData({
                        continues: !0
                    }), a.setData({
                        playStatus: 0
                    }), a.stopProgressListener();
                }
            });
        }), !1;
        e == i && this.setData({
            stepTime: "00:00",
            durationTime: "00:00"
        });
    },
    onTogglePlayTap: function(t) {
        var a = this;
        if (a.data.playStatus) wx.pauseBackgroundAudio({
            success: a.onPause,
            fail: function(t) {
                wx.showModal({
                    content: "暂停失败：" + t.errMsg,
                    showCancel: !1
                });
            }
        }); else {
            var e = wx.getStorageSync(playKey);
            a.playUrl != e.path && wx.stopBackgroundAudio(), wx.playBackgroundAudio({
                dataUrl: a.data.datum.article.bg_music,
                complete: a.onPlay
            });
        }
    },
    onReady: function(t) {
        this.audioCtx = wx.createAudioContext("myAudio"), this.videoContext = wx.createVideoContext("myVideo");
    },
    videoPlay: function(t) {
        wx.setNavigationBarTitle({
            title: "视频加载中..."
        });
    },
    videoStop: function(t) {
        var a = this, e = t.detail.currentTime;
        if (0 < e && wx.setNavigationBarTitle({
            title: 介绍
        }), !this.data.member && 1 == this.data.pay_status && !this.data.mypay && e > this.data.currentStop && this.data.userInfo.author != this.data.datum.article.author_id) return this.videoContext.seek(this.data.currentStop), 
        this.videoContext.pause(), _function.hint(3, "完成支付继续观看哦^_^!", "温馨提示", function(t) {
            a.setData({
                continues: !0
            });
        }), !1;
    },
    onPullDownRefresh: function() {
        this.getDatum(this.data.id), this.comment(this.data.id), wx.stopPullDownRefresh();
    },
    getDatum: function(t) {
        var e = this, i = this;
        _function.request("entry/wxapp/Detail", {
            id: t,
            uid: this.data.userInfo.id,
            openid: this.data.userInfo.openid
        }, "", function(t) {
            if (!t.article.id) return _function.hint(2, "抱歉,该音乐已删除,请阅览其他音乐吧~", "系统提示", function(t) {}), 
            !1;
            if (t.article.content && 2 != t.article.type && WxParse.wxParse("content", "html", t.article.content, i), 
            t.article.description && 2 != t.article.type && WxParse.wxParse("description", "html", t.article.description, i), 
            e.setData({
                datum: t,
                mypay: t.mypay,
                member: t.member,
                pay_num: t.article.pay_num,
                zan: t.article.zanNum,
                oldzan: t.zan,
                collect: t.collect,
                related: t.related,
                shang_num: t.shang,
                currentStop: t.article.bg_music_set,
                pay_status: 0 < t.article.pay_money ? 1 : 0
            }), 1 == t.pay_free_info) return _function.hint(3, "抱歉,距离上次支付超过免费观看时间请再次支付继续观看哦！", "温馨提示", function(t) {}), 
            !1;
            var a = wx.getStorageSync(playKey);
            3 == t.article.types ? 1 == e.data.is_avAutoPlay ? t.article.bg_music != a.path && (wx.stopBackgroundAudio(), 
            wx.setStorageSync(playKey, {
                id: t.article.id,
                path: t.article.bg_music
            }), wx.playBackgroundAudio({
                dataUrl: t.article.bg_music,
                complete: e.onPlay
            })) : t.article.bg_music != a.path ? (wx.onBackgroundAudioStop(e.onStop), wx.setStorageSync(playKey, {
                id: t.article.id,
                path: t.article.bg_music
            }), wx.playBackgroundAudio({
                dataUrl: t.article.bg_music,
                complete: e.onPlay
            })) : (wx.onBackgroundAudioStop(e.onStop), wx.getBackgroundAudioPlayerState({
                success: function(t) {
                    wx.getStorageSync(playKey), e.data.datum.article.bg_music;
                    e.startProgressListener();
                }
            })) : (wx.onBackgroundAudioStop(e.onStop), wx.stopBackgroundAudio());
        }, this), this.comment(t);
    },
    detail: function(t) {
        var a = t.currentTarget.dataset.id, e = t.currentTarget.dataset.url;
        t.currentTarget.dataset.type;
        _function.request("entry/wxapp/Judge", {
            uid: this.data.userInfo.id,
            id: a,
            author: this.data.userInfo.author
        }, "", function(t) {
            if (0 == t) return _function.hint(3, "抱歉,该音乐仅限指定分组用户聆听^_^!", "温馨提示", function(t) {}), 
            !1;
            wx.redirectTo({
                url: e + "?id=" + a
            });
        }, this);
    },
    comment: function(t) {
        var a = this;
        _function.request("entry/wxapp/DetailComment", {
            id: t,
            openid: this.data.userInfo.openid,
            p: this.data.p,
            pnum: this.data.pnum
        }, "", function(t) {
            a.setData({
                comment: t
            });
        }, this);
    },
    more: function(t) {
        var a = t.currentTarget.dataset.types, e = t.currentTarget.dataset.pid, i = t.currentTarget.dataset.cid;
        wx.navigateTo({
            url: "../art_list/index?types=" + a + "&cid=" + e + "," + i
        });
    },
    pay: function(t) {
        var e = this, a = wx.getStorageSync("fxid"), i = wx.getStorageSync("fx_pay_type");
        this.data.userInfo.fopenid || "scene" != i || _function.request("entry/wxapp/Binding", {
            fxid: a,
            fx_pay_type: i,
            uid: this.data.userInfo.id
        }, "", function(t) {}, this, "POST");
        var n = t.currentTarget.dataset.types, s = t.currentTarget.dataset.money, r = t.currentTarget.dataset.vid;
        a = a == this.data.userInfo.id ? "" : a, _function.request("entry/wxapp/Pay", {
            id: this.data.id,
            author_id: this.data.datum.article.author_id,
            uid: this.data.userInfo.id,
            openid: this.data.userInfo.openid,
            money: s,
            types: n,
            day: t.currentTarget.dataset.day,
            fxid: a,
            fopenid: this.data.userInfo.fopenid,
            vid: r
        }, "", function(t) {
            var a = e;
            1 == t.state ? wx.requestPayment({
                timeStamp: t.timeStamp,
                nonceStr: t.nonceStr,
                package: t.package,
                signType: "MD5",
                paySign: t.paySign,
                success: function(t) {
                    a.setData({
                        pay_status: 2
                    }), a.getDatum(a.data.id, a.data.appoint), 1 == n ? a.setData({
                        mypay: a.data.id
                    }) : 3 == n && a.setData({
                        member: 1
                    }), 3 == a.data.datum.article.types && wx.playBackgroundAudio({
                        dataUrl: a.data.datum.article.bg_music,
                        complete: a.onPlay
                    });
                },
                fail: function(t) {
                    _function.hint(3, "支付失败^_^!", "网络提示", function(t) {});
                }
            }) : _function.hint(1, "网络错误！", "", function(t) {});
        }, this, "POST");
    },
    zancollect: function(t) {
        var a = this, e = t.currentTarget.dataset.types;
        _function.request("entry/wxapp/Zancollect", {
            id: this.data.id,
            openid: this.data.userInfo.openid,
            types: e
        }, "", function(t) {
            1 == e ? a.setData({
                zan: parseInt(a.data.zan) - 1,
                oldzan: 0
            }) : 2 == e ? a.setData({
                zan: parseInt(a.data.zan) + 1,
                oldzan: a.data.id
            }) : 3 == e ? a.setData({
                collect: 0
            }) : 4 == e && a.setData({
                collect: a.data.id
            });
        }, this);
    },
    collect: function() {
        var a = this;
        _function.request("entry/wxapp/Collect", {
            id: this.data.id
        }, "", function(t) {
            a.setData({
                zan: parseInt(a.data.zan) + 1
            });
        }, this);
    },
    tel: function() {
        wx.makePhoneCall({
            phoneNumber: this.data.datum.article.tel
        });
    },
    zan: function(t) {
        var i = this, n = t.currentTarget.dataset.id;
        _function.request("entry/wxapp/Zan", {
            id: n,
            aid: this.data.id,
            openid: this.data.userInfo.openid
        }, "", function(t) {
            if (0 != t) {
                var a = i.data.comment;
                for (var e in a) a[e].id == n && (0 == t.type ? (a[e].zan = 0, a[e].zannum = parseInt(a[e].zannum) - 1) : (a[e].zan = 1, 
                a[e].zannum = parseInt(a[e].zannum) + 1));
                i.setData({
                    comment: a
                });
            } else _function.hint(1, "网络错误^_^!", 2e3, function(t) {});
            i.setData({
                comzan: t,
                comid: n
            });
        }, this);
    },
    continues: function(t) {
        this.setData({
            continues: !this.data.continues
        });
    },
    become: function(t) {
        this.setData({
            become: !this.data.become
        });
    },
    tk: function(t) {
        this.setData({
            tk: !this.data.tk
        });
    },
    shang: function(t) {
        this.setData({
            shang: !this.data.shang
        });
    },
    shangSubmit: function(t) {
        var e = this, a = t.detail.value.shang_money;
        if ("" == a) return _function.hint(3, "对不起，请输入打赏金额^_^!", "温馨提示", function(t) {}), 
        !1;
        _function.request("entry/wxapp/Shang", {
            id: this.data.id,
            shang_money: a,
            uid: this.data.userInfo.id,
            openid: this.data.userInfo.openid,
            avatar: this.data.userInfo.avatar,
            author_id: this.data.datum.article.author_id
        }, "", function(t) {
            if (1 == t.state) {
                var a = e;
                wx.requestPayment({
                    timeStamp: t.timeStamp,
                    nonceStr: t.nonceStr,
                    package: t.package,
                    signType: "MD5",
                    paySign: t.paySign,
                    success: function(t) {
                        _function.hint(3, "打赏成功^_^!", "温馨提示！", function(t) {
                            a.setData({
                                shang: !a.data.shang
                            });
                        }), _function.request("entry/wxapp/Detail", {
                            id: a.data.id,
                            types: "shang"
                        }, "", function(t) {
                            a.setData({
                                shang_num: t
                            });
                        }, this);
                    },
                    fail: function(t) {
                        _function.hint(3, "打赏失败^_^!", "网络提示", function(t) {
                            a.setData({
                                shang: !a.data.shang
                            });
                        });
                    }
                });
            } else _function.hint(3, "网络错误，请稍后重试—_—!", "温馨提示！", function(t) {
                e.setData({
                    shang: !e.data.shang
                });
            });
        }, this);
    },
    formSubmit: function(t) {
        var a = this, e = t.detail.value.content, i = t.detail.formId;
        if ("" == e) return _function.hint(3, "对不起，请输入留言内容^_^!", "温馨提示", function(t) {}), 
        !1;
        _function.request("entry/wxapp/Message", {
            id: this.data.id,
            content: e,
            openid: this.data.userInfo.openid,
            uid: this.data.userInfo.id,
            nickname: this.data.userInfo.nickname,
            avatar: this.data.userInfo.avatar,
            formId: i
        }, "", function(t) {
            1 == t ? _function.hint(3, "评论成功^_^!", "温馨提示！", function(t) {
                a.setData({
                    tk: !a.data.tk
                });
            }) : _function.hint(3, "网络错误，请稍后评论—_—!", "温馨提示！", function(t) {
                a.setData({
                    tk: !a.data.tk
                });
            });
        }, this);
    },
    index: function() {
        wx.redirectTo({
            url: "../index/index"
        });
    },
    report_tan: function() {
        this.setData({
            report_tan: !this.data.report_tan,
            radio_value: 0
        });
    },
    report: function(t) {
        this.setData({
            radio_value: t.detail.value
        });
    },
    report_submit: function(t) {
        var a = this, e = t.detail.value.radio_content;
        if (0 == this.data.radio_value) return _function.hint(1, "请选择举报类型", 2e3), !1;
        _function.request("entry/wxapp/Report", {
            radio_content: e,
            types: this.data.radio_value,
            a_id: this.data.id,
            u_id: this.data.userInfo.id,
            nickname: this.data.userInfo.nickname,
            avatar: this.data.userInfo.avatar
        }, "", function(t) {
            a.report_tan(), _function.hint(3, "举报成功，我们会尽快核实处理您的举报内容！", "温馨提示！", function(t) {});
        }, this, "POST");
    },
    onShareAppMessage: function() {
        var t = wx.getStorageSync("share_title"), a = this.data.datum.article.title;
        return this.data.userInfo.id ? {
            title: t && a ? a : t,
            path: "dg_costread/pages/newcms/detail/index?id=" + this.data.id + "&fxid=" + this.data.userInfo.id + "&fxtype=fx"
        } : {
            title: t && a ? a : t,
            path: "dg_costread/pages/newcms/detail/index?id=" + this.data.id
        };
    }
});